"""

"""


from flask import jsonify,request,session

from main.user.models import User
from main.public.models import DetailList,Product
from main.utils import SWJsonify
from flask_login_multi import login_required,current_user
from . import bp

import datetime 



@bp.route('/api/wallet_transfer',methods=['POST'])
@login_required
def wallet_transfer():
    """
    转账操作
    """
    #钱包地址
    wallet_code = request.form['wallet_code']
    #数量
    number = request.form['number']
    #交易密码
    pwd = request.form['pwd']

    wallet_code = User.query.filter_by(wallet_code=wallet_code).first()

    if not wallet_code:
        return jsonify({'code':0,'url':'','msg':'没有该钱包地址。'})
    if session.get('system_config')['mintransfer']>int(number):
        return jsonify({'code':0,'url':'','msg':'最低转账：%s.'%session.get('system_config')['mintransfer']})
    if not current_user.check_passcheck(pwd):
        return jsonify({'code':0,'url':'','msg':'交易密码不正确，请重新输入。'})

    if float(current_user.dig_income) < float(number):
        return jsonify({'code':0,'url':'','msg':'挖矿收益没有这么多。'})


    user = User.query.filter_by(wallet_code=wallet_code).firset()

    #添加明细
    DetailList.create(
        user_id = current_user.id,
        user_username = current_user.username,
        type_msg = "挖矿收益",
        change_first = current_user.dig_income,
        change_last = float(current_user.dig_income) - float(number),
        create_at = datetime.datetime.now(),
        change_user = "用户ID："+current_user.id+" 发起的转账"
    )
    current_user.update(
        change_first = float(current_user.dig_income) - float(number)
    )
    DetailList.create(
        user_id = user.id,
        user_username = user.username,
        type_msg = "钱包",
        change_first = user.wallet,
        change_last = float(user.wallet) + float(number),
        create_at = datetime.datetime.now(),
        change_user = "用户ID："+current_user.id+" 发起的转账"
    )
    user.update(
        change_first = float(user.wallet) + float(number)
    )
    

    return jsonify({'code':1,'url':'','msg':'转账完成'})




@bp.route('/api/get_minermk',methods=['GET'])
@login_required
def get_minermk():
    """获取矿市列表"""
    product = Product.query.all()
    return SWJsonify({'result':product})




